<template>
  <!-- 任务通知页面  -->
  <div class="taskNotice z-h-100">
    <div class="filterSelectWrapper">
      <filterSelect @change="changeFun" />
    </div>
    <div class="taskNotice-header">
      <el-button type="primary" size="mini" @click="newAddFun">新增</el-button>
      <el-button type="primary" size="mini" @click="upDateFun">修改</el-button>
      <el-button type="primary" size="mini" @click="viewFun">查看</el-button>
    </div>
    <div class="treeWrapper">
      <treeTable ref="treeTable" />
    </div>
    <portalDialog :dialog-visible="visible" :title="cTitle" width="60%" :show-tool-bar="false" @comfireFun="comfireFun" @defineFun="defineFun">
      <template slot="content">
        <modalForm :key="status" ref="modalForm" />
      </template>
    </portalDialog>
  </div>
</template>

<script>
import treeTable from './components/treeTable'
import { saveAll } from '@/api/portalTaskUi/taskNotice'
import { getDictType, getTenantId } from '@/api/portalTaskUi/taskList'
import portalDialog from '@/components/portalDialog'
import modalForm from './components/modalForm'
import filterSelect from '@/components/filterSelect'
export default {
  name: 'TaskNotice',
  provide() {
    return {
      root: this
    }
  },
  components: {
    treeTable,
    portalDialog,
    modalForm,
    filterSelect
  },
  data() {
    return {
      treeData: [],
      mapData: [],
      visible: false,
      status: 'add',
      currentId: '', // 下拉框id
      usersId: '', // 当前userId
      isSystemConfig: '0'
    }
  },
  computed: {
    cTitle() {
      return this.status === 'add' ? '新增任务配置' : '修改任务配置'
    }
  },
  async created() {
    const userData = await this.getTenantIdFun() // 获取用户id
    console.log(userData)
    this.currentId = userData.id
    this.usersId = userData.id
    this.isSystemConfig = this.changeRouterToSystem()
    // let fullPathArr = this.$route.fullPath.split("/");
    // let isSystemConfig = fullPathArr[fullPathArr.length - 1];
    this.$nextTick(() => {
      const params = {
        tenantId: this.currentId,
        isSystemConfig: this.isSystemConfig
      }
      this.$refs.treeTable.getSysTreeListFun(params)
    })
  },
  methods: {
    comfireFun() {
      this.visible = false
    },
    // 确定
    defineFun() {
      const data = this.$refs.modalForm.tableData
      const treeData = this.$refs.modalForm.value// 获取树的数据
      const taskRelation = this.$refs.modalForm.form.taskRelation// 获取下拉框的数据
      if (treeData.length === 0) {
        this.$message({
          type: 'warning',
          message: '任务类型不能为空!'
        })
      } else {
        const params = {
          isSystemConfig: this.isSystemConfig,
          noticeUsers: taskRelation,
          taskTypeId: treeData[0].nodeId,
          listJson: JSON.stringify(data),
          tenantId: this.currentId
        }
        saveAll(params).then(async res => {
          if (res.data.code === 0) {
            this.$message({
              message: '保存成功!',
              type: 'success'
            })
            const params = {
              tenantId: this.currentId,
              isSystemConfig: this.isSystemConfig
            }
            await this.$refs.treeTable.getSysTreeListFun(params)
          } else {
            this.$message({
              message: '保存失败!',
              type: 'error'
            })
          }
          this.visible = false
        })
      }
    },
    changeRouterToSystem() {
      const fullPathArr = this.$route.fullPath.split('/')
      const isSystemConfig = fullPathArr[fullPathArr.length - 1] || '0'
      return isSystemConfig
    },
    changeFun(value) {
      this.currentId = value
      console.log(this.usersId)

      const params = {
        tenantId: this.currentId,
        isSystemConfig: this.isSystemConfig
      }
      this.$refs.treeTable.getSysTreeListFun(params)
    },
    // 获取用户身份
    getTenantIdFun() {
      return new Promise(resolve => {
        getTenantId().then(res => {
          resolve(res.data.data)
        })
      })
    },
    // 查看按钮
    viewFun() {
      const selectData = this.$refs.treeTable.multipleSelection
      if (selectData.length === 0) {
        this.$message({
          message: '请选择元素',
          type: 'warning'
        })
      } else {
        const row = selectData[0]
        if (!row.id && row.id === null) {
          this.$message({
            message: '该任务无法查看',
            type: 'warning'
          })
          return
        }
        this.visible = true
        this.status = 'view'
        const params = Object.assign(row, {
          tenantId: this.currentId,
          status: 'view'
        })
        this.$nextTick(() => {
          console.log(this.$refs.modalForm)
          this.$refs.modalForm.setModelFormData(params)
        })
        // console.log(row);
      }
    },
    // 新增
    newAddFun() {
      const selectData = this.$refs.treeTable.multipleSelection
      console.log(selectData)
      this.visible = true
      this.status = 'add'
      const row = selectData[0] || {}
      const params = Object.assign(row, {
        tenantId: this.currentId,
        status: 'add'
      })
      this.$nextTick(() => {
        console.log(this.$refs.modalForm)
        this.$refs.modalForm.setModelFormData(params)
      })
      // if (selectData.length === 0) {
      //   this.$message({
      //     message: "请选择元素",
      //     type: "warning"
      //   });
      // } else {
      //   this.visible = true;
      //   this.status = "add";
      //   let row = selectData[0];
      //   let params = Object.assign(row, {
      //     tenantId: this.currentId,
      //     status: "add"
      //   });
      //   this.$nextTick(() => {
      //     console.log(this.$refs.modalForm);
      //     this.$refs.modalForm.setModelFormData(params);
      //   });
      // }
    },
    // 修改
    upDateFun() {
      const selectData = this.$refs.treeTable.multipleSelection
      if (selectData.length === 0) {
        this.$message({
          message: '请选择元素',
          type: 'warning'
        })
      } else {
        const row = selectData[0]
        if (!row.id && row.id === null) {
          this.$message({
            message: '该任务无法修改',
            type: 'warning'
          })
          return
        }
        this.visible = true
        this.status = 'update'
        const params = Object.assign(row, {
          tenantId: this.currentId,
          status: 'update'
        })
        this.$nextTick(() => {
          console.log(this.$refs.modalForm)
          this.$refs.modalForm.setModelFormData(params)
        })
        // console.log(row);
      }
      // this.$nextTick(() => {
      //   console.log(this.$refs.modalForm);
      //   this.$refs.modalForm.setModelFormData(params);
      // });
    },
    // getSysTreeListFun (params) {
    //   getSysTreeList(params).then(res => {
    //     if (res.data.code === 0) {
    //       let data = res.data.data;
    //       console.log(res);
    //       this.treeData = data;
    //     } else {
    //       this.treeData = [];
    //     }
    //   });
    // },
    // 获取类型的字典
    getDictTypeFun(params) {
      // 字典名称
      return new Promise(resolve => {
        getDictType(params).then(res => {
          resolve(res.data.data)
        })
      })
    }
  }

}
</script>

<style lang="less" scoped>
.filterSelectWrapper{
  width: 100%;
  margin-bottom: 10px;
}
.taskNotice{
  padding:10px;
  background-color:#fff;
  .taskNotice-header{
    margin-bottom: 10px;
  }
}
.treeWrapper{
  height: calc(100% - 40px - 40px);
}
</style>
